package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.utils.ArrayUtils;
import com.xsherl.leetcode.utils.PrintUtils;

public class SetMatrixZeroes {

    /**
     * 遍历元素，如果此元素为0，说明这一行和这一列都是0，对行和列分别做标记
     * 然后修改行或列被标记了的元素为0
     */
    public void setZeroes(int[][] matrix) {
        boolean[] zeroRow = new boolean[matrix.length];
        boolean[] zeroCol = new boolean[matrix[0].length];
        for (int i = 0; i < matrix.length; ++i){
            for (int j = 0; j < matrix[0].length; ++j){
                if (matrix[i][j] == 0){
                    zeroRow[i] = true;
                    zeroCol[j] = true;
                }
            }
        }
        for (int i = 0; i < matrix.length; ++i){
            for (int j = 0; j < matrix[0].length; ++j){
                if (zeroRow[i] || zeroCol[j]){
                    matrix[i][j] = 0;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = ArrayUtils.parseArray("" +
                "[[0,0,0,5],[4,3,1,4],[0,1,1,4],[1,2,1,3],[0,0,1,1]]" +
                "", int[].class);
        PrintUtils.println(matrix);
        PrintUtils.println("=========================");
        new SetMatrixZeroes().setZeroes(matrix);
        PrintUtils.println(matrix);
    }

}
